﻿using System;
using System.IO;
using AgFx;
using DepSoft.Mobile.WP7.FollwIt.Model;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace DepSoft.Mobile.WP7.FollwIt.ViewModel
{
    /// <summary>
    /// We cache the Stream for 2 minutes ( 120 seconds )
    /// </summary>
    [CachePolicy(CachePolicy.CacheThenRefresh, 120)]
    public class agfxStreamViewModel : ModelItemBase<StreamLoadContext>
    {
        #region Public bindable properties
        /// <summary>
        /// The <see cref="Stream" /> property's name.
        /// </summary>
        public const string StreamPropertyName = "Stream";
        private StreamItem _stream = new StreamItem();

        /// <summary>
        /// Gets the Stream property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public StreamItem Stream
        {
            get
            {
                return _stream;
            }

            set
            {
                if (!ReferenceEquals(_stream, null))
                {
                    if (_stream != value)
                        _stream = value;
                }
                RaisePropertyChanged(StreamPropertyName);
            }
        }
        #endregion

        #region Constructor
        public agfxStreamViewModel()
        {

        }

        public agfxStreamViewModel(Friend friend)
            : base(new StreamLoadContext(friend))
        {

        }
        #endregion

        #region AgFx DataLoader
        public class agfxStreamViewModelLoader : IDataLoader<StreamLoadContext>
        {
            #region IDataLoader implementation
            public object Deserialize(StreamLoadContext loadContext, Type objectType, System.IO.Stream stream)
            {
                var sr = new StreamReader(stream);
                var userProfileJSON = sr.ReadToEnd();
                JObject userProfile = JObject.Parse(userProfileJSON);

                agfxStreamViewModel viewModel = new agfxStreamViewModel(loadContext.Friend);

                JsonSerializer serializer = new JsonSerializer();

                FollwitAPIResponse response = (FollwitAPIResponse)serializer.Deserialize(new JTokenReader(userProfile), typeof(FollwitAPIResponse));

                if (ReferenceEquals(response.Response, null))
                {
                    viewModel.Stream.Friend = loadContext.Friend;


                    if (userProfile["watched_movie"].HasValues)
                    {
                        Movie watchedMovie = (Movie)serializer.Deserialize(new JTokenReader(userProfile["watched_movie"]), typeof(Movie));
                        viewModel.Stream.WatchedMovie = watchedMovie;
                    }

                    if (userProfile["watched_episode"].HasValues)
                    {
                        TVShow watchedTVShow = (TVShow)serializer.Deserialize(new JTokenReader(userProfile["watched_episode"]), typeof(TVShow));
                        viewModel.Stream.WatchedTVShow = watchedTVShow;
                    }
                }
                else
                    viewModel.Stream = new StreamItem();

                return viewModel;
            }

            public LoadRequest GetLoadRequest(StreamLoadContext loadContext, Type objectType)
            {
                return new WebLoadRequest(loadContext, new Uri(FollwItApi.GetInstance().GetActionRestURI(RestAction.GetUserProfile, loadContext.UserName)));
            }
            #endregion
        }
        #endregion
    }
}
